<?php
/**
 * Created by PhpStorm.
 * User: woddp
 * Date: 2016/8/18
 * Time: 21:51
 */

namespace App\Controllers\Stat;

use App\Controllers\BaseController;
use App\Library\Helper;
use App\Models\SmsWlistLog;

class WlistlogController extends BaseController {
    public function initialize() {
        parent::initialize();
        if($this->request->isPost()) {
            $this->checkLogin();
        }
    }
    /**
     * 积分列表展示
     */
    public function indexAction() {

        $this->display('stat/wlistlog/index');
    }
    //---------------------------------------------------------------------------------
    //统计积分日志总数
    //---------------------------------------------------------------------------------
    public function countAction() {
        $smsWlistModel = new SmsWlistLog();
        $ret           = $smsWlistModel->total($this->parseFilter());
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取总数失败，系统错误！");
        }
    }
    //---------------------------------------------------------------------------------
    //获取特别互动内容
    //---------------------------------------------------------------------------------
    public function getsAction() {
        $data = [
            'page' => $this->request->getPost('page'),
            'size' => $this->request->getPost('size'),
        ];
        if($data['page'] == '' || !is_numeric($data['page'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前的页数！');
        }
        if($data['size'] == '' || !is_numeric($data['size'])) {
            return $this->sendAjax(false, '获取数据失败，未获取到当前指定每页条数！');
        }
        $data['page'] = intval($data['page']);
        $data['size'] = intval($data['size']);

        $offset        = ($data['page'] - 1) * $data['size'];
        $smsWlistModel = new SmsWlistLog();
        $ret           = $smsWlistModel->gets($this->parseFilter(), $offset, $data['size']);
        if($ret !== false) {
            return $this->sendAjax(true, $ret);
        } else {
            return $this->sendAjax(false, "获取数据失败，系统错误！");
        }
    }


    //---------------------------------------------------------------------------------
    //查看积分具体的信息
    //---------------------------------------------------------------------------------
    public function detailAction() {
        if($this->request->isGet()) {
            $this->display('/stat/wlistlog/detail');
        } else {
            $id = $this->request->getPost('id') != '' ? $this->request->getPost('id') : $this->request->getQuery('id');
            if($id != '' && is_numeric($id)) {
                $smsWlistModel       = new SmsWlistLog();
                $entity              = $smsWlistModel->get($id);
                $entity['mt_status'] = $entity['mt_status'] == 200 ? '发送成功' : '发送失败';
                return $this->sendAjax(true, $entity);
            } else {
                return $this->sendAjax(false, '获取数据失败，参数不正确！');
            }
        }
    }


    //----------------------------------------------------------------
    //下载积分内容
    //----------------------------------------------------------------
    public function downloadAction() {
        ini_set('memory_limit', '2G');
        set_time_limit(0);
        $status     = isset($_GET['status']) ? $_GET['status'] : '';
        $mobile     = isset($_GET['mobile']) ? trim($_GET['mobile']) : '';
        $start_date = isset($_GET['start_date']) ? $_GET['start_date'] : '';
        $end_date   = isset($_GET['end_date']) ? $_GET['end_date'] : '';

        $start_date = $start_date != '' ? Helper::getDatepickerDateToStandardDate($start_date) : '';
        $end_date   = $end_date != '' ? Helper::getDatepickerDateToStandardDate($end_date) : '';

        $where = [];
        if($status != '') {
            $where['mt_status'] = $status;
        }
        if($mobile != '') {
            $where['mobile'] = $mobile;
        }
        if($start_date != '') {
            $where['start_date'] = $start_date;
        }
        if($end_date != '') {
            $where['end_date'] = $end_date;
        }
        $smsWlistModel       = new SmsWlistLog();
        $ret = $smsWlistModel->getsByDownload($where);
        foreach($ret as $k => $v) {
            if($v['status'] == 1) {
                $ret[$k]['status'] = '成功';
            } else {
                $ret[$k]['mt_status'] = '失败';
            }
            $ret[$k]['message']        = Helper::formatDownloadText($v['message']);
            $ret[$k]['mt_api_message'] = Helper::formatDownloadText($v['mt_api_message']);
        }
        if($ret !== false) {
            $head = "手机号码,上行内容,下行内容,短信长度,计费条数,状态,创建时间\r\n";
            $tpl  = "%s,%s,%s,%s,%s,%s,%s\r\n";
            $data = '';
            $data .= $head;
            foreach($ret as $v) {
                $data .= sprintf($tpl, $v['mobile'], $v['message'], $v['mt_api_message'], $v['mt_length'], $v['mt_fee_count'], $v['mt_status'], $v['create_at']);
            }
            header("Pragma: public");
            header("Expires: 0");
            header("Content-type: application/octet-stream");
            header("Accept-Ranges: bytes");
            header("Accept-Length: " . strlen($data));
            header("Content-Disposition: attachment; filename=" . iconv("UTF-8", "GB2312//TRANSLIT", "积分日志.csv"));
            echo mb_convert_encoding($data, 'GBK', 'UTF-8');
            exit;
        } else {
            $this->sendAjax(false, "获取数据为空，或者系统错误！");
        }
    }




    //---------------------------------------------------------------------------------
    //搜索积分条件筛选
    //---------------------------------------------------------------------------------
    private function parseFilter() {
        $where = $this->request->getPost('filter');

        if($where['mobile'] != '') {
            $where['mobile'] = $where['mobile'];
        } else {
            unset($where['mobile']);
        }
        if($where['status'] != '') {
            $where['status'] = $where['status'];
        } else {
            unset($where['status']);
        }
        if($where['start_date'] != '') {
            $where['start_date'] = Helper::getDatepickerDateToStandardDate($where['start_date']);
        } else {
            unset($where['start_date']);
        }
        if($where['end_date'] != '') {
            $where['end_date'] = Helper::getDatepickerDateToStandardDate($where['end_date']);
        } else {
            unset($where['end_date']);
        }
        return $where;
    }

}